package com.dearbinge.data.service.impl.uweShare;

import com.dearbinge.data.api.uweShare.UweShareService;
import com.dearbinge.data.dao.ShowFieldMapper;
import com.dearbinge.data.dao.UserMapper;
import com.dearbinge.data.dao.UweShareMapper;
import com.dearbinge.data.pojo.showField.ShowField;
import com.dearbinge.data.pojo.thumbs.Thumbs;
import com.dearbinge.data.pojo.user.User;
import com.dearbinge.data.pojo.uweShare.UweShare;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "uweShareService")
public class UweShareServiceImpl implements UweShareService {

    @Autowired
    private UweShareMapper uweShareMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShowFieldMapper showFieldMapper;

    private Integer dataCode = 0;


    @Override
    public Integer update(UweShare uweShare) {
        dataCode = uweShareMapper.update(uweShare);
        return dataCode;
    }

    @Override
    public Integer insert(UweShare uweShare) {
        dataCode = uweShareMapper.insert(uweShare);
        try {
            if (dataCode>0){
                ShowField sf = new ShowField();
                ShowField showField = showFieldMapper.getById(Integer.valueOf(uweShare.getSourceId()));
                Integer thumbsCount = 1;
                if (showField.getThumbsCount() != null) {
                    thumbsCount = Integer.valueOf(showField.getUweShareCount());
                    thumbsCount++;
                }
                sf.setId(showField.getId());
                sf.setUweShareCount(thumbsCount);
                showFieldMapper.update(sf);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return dataCode;
    }

    @Override
    public Integer delete(Integer id) {
        dataCode = uweShareMapper.delete(id);
        return dataCode;
    }

    @Override
    public List<Map<String,Object>> getByIdAndFromType(Integer id, Integer fromType) {
        List<UweShare> list = uweShareMapper.getByIdAndFromType(id,fromType);
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("uweShare",list);
        map.put("user",null);
        mapList.add(map);
        if (list != null && list.size() != 0){
            mapList = getUserById(list,map);
        }
        return mapList;
    }

    @Override
    public Map<String,Object> getById(Integer id) {
        List<UweShare> list = uweShareMapper.getById(id);
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("uweShare",list);
        map.put("user",null);
        mapList.add(map);
        if (list != null && list.size() != 0) {
            mapList = getUserById(list,map);
        }
        return mapList.get(0);
    }

    private List<Map<String,Object>> getUserById(List<UweShare> list,Map<String,Object> map){
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        try {
            for (int index = 0;index < list.size();index++){
                map = new HashMap<String, Object>();
                map.put("uweShare",list.get(index));
                int userId = Integer.parseInt(list.get(index).getUserId());
                User user = userMapper.getById(userId);
                map.put("user",null);
                if (user != null) {
                    map.put("user",user);
                }
                mapList.add(map);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return mapList;
    }
}
